package com.zjw.zy.radio

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import com.zjw.zy.R
import com.zjw.zy.base.BaseActivity
import com.zjw.zy.coreui.view.RadioProgressView
import com.zjw.zy.coreui.view.RadioView

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/12/29 15:51
 * @description：
 */
class RadioActivity : BaseActivity() {

    companion object {
        private const val HIDE_OPERATE_DELAY = 3000L

        private const val HIDE_OPERATE_MSG = 1
    }

    private val surface: RadioView by lazy { findViewById(R.id.radio_activity_main) }

    private val progressView: RadioProgressView by lazy { findViewById(R.id.radio_activity_progress) }

    private var isShowOperate = false

    private val handler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                HIDE_OPERATE_MSG -> {
                    if (isShowOperate) {
                        toggleOperate()
                    }
                }
            }
        }
    }

    private fun toggleOperate() {
        handler.removeMessages(HIDE_OPERATE_MSG)

        progressView.visibility = if (isShowOperate) {
            View.GONE
        } else {
            handler.sendEmptyMessageDelayed(HIDE_OPERATE_MSG, HIDE_OPERATE_DELAY)
            View.VISIBLE
        }
        isShowOperate = !isShowOperate
    }

    override fun onBindListener() {

        surface.setOnClickListener {
            toggleOperate()
        }

        surface.bindListener(object : RadioView.Listener {
            override fun onProgress(progress: Int) {
                progressView.updateProgress(progress)
            }

            override fun onProgressTotal(total: Int) {
                progressView.updateMax(total)
            }

        })

        progressView.bindListener(object : RadioProgressView.Listener {
            override fun onPlay() {
                surface.resume()
            }

            override fun onPause() {
                surface.pause()
            }

            override fun onProgressChange(position: Int, total: Int) {
                surface.progress(position)
                progressView.updateProgress(position)
            }

            override fun onProgressStartChange() {
                surface.pause()
                handler.removeMessages(HIDE_OPERATE_MSG)
            }

            override fun onProgressEndChange() {
                if (progressView.isPlay()) {
                    surface.resume()
                }
                handler.sendEmptyMessageDelayed(HIDE_OPERATE_MSG, HIDE_OPERATE_DELAY)
            }
        })
    }

    override fun onBindValue() {
        progressView.visibility = View.GONE

        //todo 这儿先写死
        surface.play("https://flv2.bn.netease.com/fd5f066da93cc5c0248765eeb07ed6dda87141ca47a0a8dc0944dc8ea6ca9d8bba2e7907de3713e64a103b6b61818aa9751a94d9ed5f778ab56cb2d45f82aabca3ebc2c7a9de7a621573d66b285c47cb79968004fc1ff4fbf6f9e82b07bcdd6916a3077bd0dd9b52607e48a1124756cc005beff4d7defd3e.mp4")
    }

    override fun onFindView() {

    }

    override fun onLayoutId(): Int {
        return R.layout.radio_fragment_main
    }
}